package com.jz.netthings.bloodsources.service.impl;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.application.bodyCheck.bean.MyPage;
import com.jz.netthings.bloodsources.bean.XyLink;
import com.jz.netthings.bloodsources.mapper.XyLinkMapper;
import com.jz.netthings.bloodsources.service.XyLinkService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.utils.Token;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 血源链接表
----表关系↓
--- 血源公告牌鉴权表
             |
---血源链接表
          ---提醒记录表
          ---未献浆记录表
          ---回访记录表
 
 服务实现类
 * </p>
 *
 * @author 张凡
 * @since 2019-08-03
 */
@Service
public class XyLinkServiceImpl extends ServiceImpl<XyLinkMapper, XyLink> implements XyLinkService {
    @Autowired
    private XyLinkMapper xyLinkMapper;
    @Override
    public MyPage<XyLink> queryByCondition(   String useNurseDeptId,Integer useNurseId,String providerAddress, String nurseName, String providerName, String providerActiveLevel, String useVolunteer,  String isPulpingMsg,String currentPage, String pageSize) throws AdscmSystemException {
        //进行分页数据判定
        MyPage<XyLink> myPage=new MyPage<XyLink>();
        myPage.setCurrentPageIndex(Integer.parseInt(currentPage));
        myPage.setPageSize(Integer.parseInt(pageSize));
        //对当前页参数currentPage 进行重新定义，为节省JVM资源开销，直接将计算后的开始页赋值给参数变量currentPage = beginIndex
        currentPage = myPage.getBeginIndex()+"";
        Integer count = xyLinkMapper.queryByCount(useNurseDeptId,useNurseId,providerAddress, nurseName, providerName, providerActiveLevel,useVolunteer);
        List<XyLink> list  = xyLinkMapper.queryByCondition(useNurseDeptId,useNurseId,providerAddress, nurseName,"", providerName, providerActiveLevel,useVolunteer,null,Integer.parseInt(currentPage),Integer.parseInt(pageSize));
        //获取相关数据
        myPage.setItems(list);
        myPage.setTotal(count);
        return myPage;
    }

    @Override
    public List<XyLink> loadXyLinkToCache(String pulpingMsgType) throws AdscmSystemException {
        List<XyLink> list  = xyLinkMapper.loadXyLinkToCache(pulpingMsgType);
        return list;
    }

    @Override
    public Boolean insertXyLinkInfo(XyLink xyLink, Token token) throws AdscmSystemException {
        boolean insert = false;
        synchronized (this) {
            if (!AdscmBaseUtils.getInstance().isNotEmpty(xyLink.getUseNurseName(), "")) {
                throw new AdscmSystemException("护士不能为空!");
            }
            if (!AdscmBaseUtils.getInstance().isNotEmpty(xyLink.getLinkProviderName(), null)) {
                throw new AdscmSystemException("浆员不能为空!");
            }
            //根据链接浆员ID 进行数据库当前浆员ID记录去重确认 ，如果有记录则提醒当前浆员已有链接记录，如果没有则继续添加linkProviderId
            if(xyLink.getLinkProviderId()!=null){
                List<XyLink> list =  xyLinkMapper.queryByCondition(null,null,null, null,xyLink.getLinkProviderId()+"", null, null,null,null,0,10);
                if(list!=null&&list.size()>=1){
                    //证明已经有该浆员ID的血源链接记录了
                }else{
                    //证明没有该浆员ID的血源链接记录，可以新增
                    insert = super.insert(xyLink);
                }
            }else {
                throw new AdscmSystemException("浆员ID【ProviderId】不能为空!");
            }
        }
        return insert;
    }

    @Override
    public Boolean updateXyLinkInfo(XyLink xyLink, Token token) throws AdscmSystemException {
        boolean b = super.updateById(xyLink);
        return b;
    }

    @Override
    public Boolean updateByProviderId(XyLink xyLink) throws AdscmSystemException {
        return xyLinkMapper.updateByProviderId(xyLink);
    }
}
